<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var waysToPartition = function (nums, k) {
      let lMap = new Map();
      let rMap = new Map();
      let sum = 0;
      for (let num of nums) {
        sum += num;
      }
      let rDiff = sum;
      let lDiff = -sum;
      let max = 0;
      for (let i = 0; i < nums.length; i++) {
        lDiff += 2 * nums[i];
        if (i == nums.length - 1) max = lMap.get(0) || 0;
        lMap.set(lDiff, (lMap.get(lDiff) || 0) + 1);
      }
      rMap.set(rDiff, -1)
      for (let i = nums.length - 1; i >= 0; i--) {
        let tmp = k - nums[i];
        lMap.set(lDiff, (lMap.get(lDiff) || 0) - 1);
        rMap.set(rDiff, (rMap.get(rDiff) || 0) + 1);
        lDiff -= 2 * nums[i];
        rDiff -= 2 * nums[i];
        max = Math.max((rMap.get(-tmp) || 0) + (lMap.get(tmp) || 0), max);
      }
      return max;
    };
    console.log(waysToPartition([2, -1, 2], 3));
  </script>
</body>

</html>